package com.jinmdz.fmis.dao.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 费用结算Provider
 *
 * @author LiCongLu
 * @date 2020-02-21 14:50
 */
public class TransportTaskProvider {

    /**
     * 按照主键获取接运任务
     *
     * @param id 接运任务主键
     * @return
     * @author LiCongLu
     * @date 2020-02-28 13:41
     */
    public String getTransportTaskViewById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append("  id,business_code,transport_purpose_code,appointment_time,transport_car_type_code,transport_car_id,transport_car_no ");
                builder.append(" ,driver_user_id,driver_user_name,driver_user_telephone,transport_place,transport_linkman,transport_telephone,departure_time,return_time ");
                builder.append(" ,process_state_code,as_empty_car,transport_distance,rule_number,rule_unit,rule_code,rule_remark,remark,random_code,charge_id,version ");

                // 关联抬尸工
                builder.append(" ,( select group_concat(system_user.full_name) from transport_task_porter ");
                builder.append("   LEFT JOIN system_user on system_user.id = transport_task_porter.system_user_id ");
                builder.append("   where transport_task_porter.deleted=0 and transport_task_id = transport_task.id ");
                builder.append("  ) as transport_task_porter_names ");

                SELECT(builder.toString());
                FROM(" transport_task ");
                WHERE(" transport_task.deleted = 0 ");
                WHERE(" transport_task.id = #{id} ");
                ORDER_BY(" transport_task.id limit 1 ");
            }
        }.toString();
    }

    /**
     * 按照业务编码查询所有接运任务
     *
     * @param businessCode 业务编码
     * @return
     * @author LiCongLu
     * @date 2020-02-28 13:57
     */
    public String listTransportTaskViewByBusinessCode(@Param("businessCode") String businessCode) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append("  id,business_code,transport_purpose_code,appointment_time,transport_car_type_code,transport_car_id,transport_car_no ");
                builder.append(" ,driver_user_id,driver_user_name,driver_user_telephone,transport_place,transport_linkman,transport_telephone,departure_time,return_time ");
                builder.append(" ,process_state_code,as_empty_car,transport_distance,rule_number,rule_unit,rule_code,rule_remark,remark,random_code,charge_id,version ");

                // 关联抬尸工
                builder.append(" ,( select group_concat(system_user.full_name) from transport_task_porter ");
                builder.append("   LEFT JOIN system_user on system_user.id = transport_task_porter.system_user_id ");
                builder.append("   where transport_task_porter.deleted=0 and transport_task_id = transport_task.id ");
                builder.append("  ) as transport_task_porter_names ");

                SELECT(builder.toString());
                FROM(" transport_task ");
                WHERE(" transport_task.deleted = 0 ");
                WHERE(" transport_task.business_code = #{businessCode} ");
                ORDER_BY(" transport_task.id ");
            }
        }.toString();
    }
}
